package com.harmontronics.hanma.paas.paas.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.PermActionList;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.RolePage;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.RoleSingle;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmRolePO;
import com.harmontronics.hanma.paas.paas.dto.iam.input.RoleCreate;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmRoleAppService;
import com.harmontronics.hanma.paas.paas.dto.iam.output.HmRoleResult;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmRoleUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmRole;

import javax.validation.Valid;


/**
 * 角色
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-07 10:35:44
 */
@RestController
@RequestMapping("/api/hmrole")
@Api(tags = {"角色"})
public class HmRoleController {
    @Autowired
    private HmRoleAppService hmRoleappService;

    /**
     * 列表
     */
    @GetMapping("/role-perms")
    @ApiOperation("根据应用权限列表")
    public ResponseEntity<List<PermActionList>> list(@RequestParam Long appId) {
        List<PermActionList> hmRoleList = hmRoleappService.selectRolePermWithAppId(appId);
        return ResponseEntity.ok(hmRoleList);
    }

    /**
    * 分页列表
    */
    @GetMapping("/page/list")
    @ApiOperation("获取所有的角色")
    public ResponseEntity<RolePage> list(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer status) {
        Page<HmRolePO> pageable = new Page(pageNo, pageSize);
        RolePage hmRolePageBase = hmRoleappService.selectRoleListWithNameAndStatus(pageable,name,status);
        return ResponseEntity.ok(hmRolePageBase);
    }

    @GetMapping("/role-perms-id")
    @ApiOperation(value = "根据角色id获取对应的权限列表")
    public ResponseEntity<List<PermActionList>> selectRolePermWithId(
            @RequestParam(required = false) Long id) {
        return ResponseEntity.ok(hmRoleappService.selectRolePermWithId(id));
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("根据Id获取具体的角色信息")
    public ResponseEntity<HmRoleResult> info(@RequestParam Long id) {
        HmRole hmRole =hmRoleappService.findById(id);
            HmRoleResult result = BeanUtil.toBean(hmRole,HmRoleResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<Long> save(@Valid @RequestBody RoleCreate hmRoleInput,BindingResult bindingResult) {
        RoleSingle hmRole =BeanUtil.toBean(hmRoleInput,RoleSingle.class);
        Long roleId = hmRoleappService.createRoleAndPermission(hmRole);

        return ResponseEntity.ok(roleId);
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody RoleCreate hmRoleInput, BindingResult bindingResult) {
        RoleSingle hmRole =BeanUtil.toBean(hmRoleInput,RoleSingle.class);
            hmRoleappService.updateRoleAndPermission(hmRole);
        return ResponseEntity.ok();
    }

    @PutMapping("/status")
    @ApiOperation(value = "禁用/启用角色")
    public ResponseEntity changeStatus(@RequestBody HmRoleUpdateInput statusDto) {
        HmRole hmRole =BeanUtil.toBean(statusDto,HmRole.class);
        hmRoleappService.changeStatus(hmRole);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmRoleappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

    @GetMapping("/validate-field")
    @ApiOperation(value = "表单字段验证")
    public ResponseEntity<String> validateField(
            @RequestParam long id,
            @RequestParam String key,
            @RequestParam String value) {
        hmRoleappService.validateField(id, key, value);
        return ResponseEntity.ok();
    }

}
